from marajo.appengine.ext import db
from marajo.appengine.api.users import User
from resources.project import Project
from resources.release import Release
from resources.client import Client

class Ticket(db.Model):
    """
    Representation of a ticket within 
    this system
    """

    name = db.StringProperty(verbose_name="Name")
    type = db.StringProperty(choices=('defect', 'enhancement', 'task', ''), verbose_name="Type")
    description = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True, verbose_name="Date Submitted")
    project = db.ReferenceProperty(Project, collection_name="tickets", verbose_name="Project")
    release = db.ReferenceProperty(Release, collection_name="tickets", verbose_name="Release")
    hours = db.IntegerProperty(default=0, verbose_name = "Hours Spent")
    estimated_hours = db.IntegerProperty(verbose_name="Estimated Hours")
    status = db.StringProperty(choices = ('open', 'closed', ''), default="open", verbose_name="Status")
    owner = db.ReferenceProperty(User, collection_name="owned_tickets", verbose_name="Owner")
    submitted_by = db.ReferenceProperty(User, collection_name="submitted_tickets", verbose_name="Submitted By")
    date_finished = db.DateTimeProperty()
    rate = db.StringProperty(choices = ("No Charge", "Standard", "Premium"), default="Standard")
    clients = db.ListProperty(Client, verbose_name="Clients")

    def has_client(self, client):
        for c in self.clients:
            if client.id == c.id:
                return True
        return False
